<template>
  <div>
    <van-nav-bar
      title="我的地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />

    <van-address-list
      v-model="choose"
      :list="list"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
  import * as get from '@/api/getProList.js'
  export default {
    data() {
      return {
        choose: '',
        list: []
      }
    },

    methods: {
      onAdd() {
        this.$router.push({name:'addAddr',query:{from:'add'}});
      },
      onEdit(item, index) {
        this.$router.push({name:'addAddr',query:{from:'change',id:item.addrId}})
      },
      onClickLeft(){
        this.$router.back(-1)
      }
    },
    mounted(){
      get.getAddr({
        per:20
      }).then(res=>{
        if(res.status == 200){
          let msg = res.data.addresses;
          this.list=msg.map((addr,index)=>{
            return {
              id:index+'',
              name:addr.receiver,
              tel:addr.mobile,
              address:addr.regions+''+addr.address,
              addrId:addr._id
            }
          })
        }else{
        }
      })
    },
    watch:{
      'choose':function(){
        let index = Number(this.choose);
        let msg=this.list[index];
        this.$router.push({name:'Cars',query:{name:msg.name,address:msg.address}});
      }
    }
  }
</script>

<style>
</style>
